<?php namespace App\Models\Data;

/**
 * Created by PhpStorm.
 * User: Will
 * Date: 6/3/16
 * Time: 10:00 AM
 */

use App\Models\Admin\Admin;
use App\Models\BaseModel;
use App\Models\NotFound;
use App\Models\StoreError;
use Illuminate\Database\Eloquent\Model;

/**
 * 驾照类型
 * Class LicenceType
 * @package App\Models\Data
 * @property $name string
 */
class LicenceType extends BaseModel {
    protected $table   = 'licence_type';
    protected $guarded = ['id'];

    const TYPE_CERTIFICATE = 0;
    const TYPE_SCENE       = 1;

    public static function getName($id) {
        $types = self::all();
        $type  = $types->where('id',$id)->first();
        return $type ? $type->name : '';
    }

    public static function all($columns = ['*']) {
        if($columns == ['*']) {
            $types = \Cache::remember('all_licence_type',60,function(){
                return parent::all();
            });
            return $types;
        } else {
            return parent::all($columns);
        } // TODO: Change the autogenerated stub
    }

    /**
     * 某校能进行此类驾照教学的教练
     *
     * @param $school_id
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function admins($school_id) { return $this->belongsToMany(Admin::class,'admin_licence_type','id','admin_id')->wherePivot('school_id', $school_id); }

    /**
     * 某校此类驾照对应的车辆
     *
     * @param $school_id
     *
     * @return mixed
     */
    public function vehicles($school_id)
    {
        $key = $this->primaryKey;

        return $this->hasMany('App\Models\Vehicle\Vehicle', $key, $key)->where('school_id', $school_id);
    }
}